package com.zbc.domain.common;

import com.zbc.enums.ErrorCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一响应结果
 * @author fangyu
 * @version v1.0.0
 * @since 2025/9/1 22:37
 */
@Data
public class Result<T> implements Serializable {
    /**
     * 响应码
     */
    private Integer code;

    /**
     * 描述信息
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(int code, T data) {
        this(code, "", data);
    }

    public Result(ErrorCode errorCode) {
        this(errorCode.getCode(), errorCode.getMessage(), null);
    }

    public static <T> Result<T> success() {
        return new Result<T>(0, "OK", null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(0, "OK", data);
    }

    public static <T> Result<T> error(int code, String msg) {
        return new Result<>(code, msg, null);
    }

    public static Result<?> error(ErrorCode errorCode) {
        return new Result<>(errorCode);
    }

    public static Result<?> error(ErrorCode errorCode, String msg) {
        return new Result<>(errorCode.getCode(), msg, null);
    }
}